#include "MonsterAI.h"

#include "Monster.h"
#include "Tile.h"


MonsterAI::MonsterAI(const Monster& monster, const Map& map) :
	monster(monster),
	map(map)
{
}


MonsterAI::~MonsterAI()
{
}


void MonsterAI::getRandomOtherValidDirection(int x, int y, int& hDirection, int& vDirection) const
{
	std::vector<int> validDirections;
	// Look for a random new direction.
	for (int i = 0; i < 3; ++ i)
	{
		getTurnedRight(hDirection, vDirection);
		unsigned int newX = x + hDirection;
		unsigned int newY = y + vDirection;
		if (map.isWithinBounds(newX, newY) &&
			map[newY][newX]->isPassable(&monster))
		{
			validDirections.push_back(hDirection);
			validDirections.push_back(vDirection);
		}
	}
	if (!validDirections.empty())
	{
		int i = rand() % (validDirections.size() / 2);
		hDirection = validDirections[i * 2];
		vDirection = validDirections[i * 2 + 1];
	}
}


void MonsterAI::getTurnedRight(int& hDirection, int& vDirection)
{
	int tempHDirection = hDirection;
	hDirection = - vDirection;
	vDirection = tempHDirection;
}
